from decimal import Decimal
from orders.models import OrderItem

def calculate_order_total(order_items):
    """Calculate the total amount for a list of order items."""
    return sum(
        item.price * Decimal(str(item.quantity))
        for item in order_items
    )

def create_order_items(order, items_data):
    """Create order items from the provided data."""
    order_items = []
    for item_id, quantity in items_data.items():
        if item_id.startswith('item_') and quantity and int(quantity) > 0:
            item_id = int(item_id.split('_')[1])
            order_item = OrderItem.objects.create(
                order=order,
                menu_item_id=item_id,
                quantity=int(quantity)
            )
            order_items.append(order_item)
    return order_items